package cn.jinjimi.domain.data.imp;

import cn.jinjimi.app.controller.event.DataEvent;
import cn.jinjimi.app.controller.event.FetchCompanyEvent;
import cn.jinjimi.domain.data.Store;
import cn.jinjimi.domain.data.net.API;
import cn.jinjimi.domain.data.net.AppService;
import cn.jinjimi.domain.store.AppStore;
import cn.jinjimi.app.entity.Companies;

import java.io.IOException;

import retrofit2.Response;

/**
 * Created by zhenhua on 16/8/11.
 */
public class AppStoreImp extends Store implements AppStore {

    @Override
    public Companies getCompanies(int type) throws IOException {

        Companies data = null;
        FetchCompanyEvent event = new FetchCompanyEvent();
        Response<Companies> response = null;


        if(COMPANY_HOT_TYPE == type){
            response = API.create(AppService.class).getHotCompanies().execute();
            event.type = FetchCompanyEvent.TYPE_HOT;
        }

        if(COMPANY_ALL_TYPE == type){
            response =  API.create(AppService.class).getCompanies().execute();
            event.type = FetchCompanyEvent.TYPE_ALL;
        }

        if(response != null && response.isSuccessful()) {
            data = response.body();
        }

        event.data = data;
        event.state = data == null? DataEvent.FAIL : DataEvent.SUCC;
        event.msg = data == null? (response == null ? "" : response.message()) : data.returnMsg;

        dispatch(event);

        return data;
    }
}
